﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class UserControl_ProductsList : System.Web.UI.UserControl
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //商品分類NodeId=3
    private int m_ProductNodeId = 3;

    private int m_NodeId = 0;
    private string m_ProductsPath = "products.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_NodeId = QueryStringHelper.GetInteger("node", 0); 

        if (!IsPostBack)
        {
            fillRepeaterProductNodes();
        }
    }

    private void fillRepeaterProductNodes()
    {
        IList<NodeVO> nodeList = m_PostService.GetNodeListByParentId(m_ProductNodeId);
        if (m_NodeId == 0 && nodeList != null && nodeList.Count > 0)
        {
            Response.Redirect(string.Format("{0}?node={1}", m_ProductsPath, nodeList[0].NodeId));
        }

        if (nodeList != null && nodeList.Count > 0)
        {
            RepeaterProducts.DataSource = nodeList.OrderBy(p => p.SortNo);
            RepeaterProducts.DataBind();
        }
    }

    protected void RepeaterProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            NodeVO nodeVO = (NodeVO)e.Item.DataItem;

            if (m_NodeId == nodeVO.NodeId)
            {
                Repeater RepeaterProductSubs = (Repeater)e.Item.FindControl("RepeaterProductSubs");
                RepeaterProductSubs.DataSource = m_PostService.GetPostListByNodeId(nodeVO.NodeId, true, "SortNo", false);
                RepeaterProductSubs.DataBind();
            }
        }
    }
}